layui.use(['layer', 'element'], function () {
    var $ = layui.jquery;
})

// 复制链接
function copy(felu) {
    var Url = document.getElementById(felu);
    Url.select(); // 选择对象
    document.execCommand("Copy"); // 执行浏览器复制命令
    layer.msg('复制成功');
}

// 下载文件
function downloadVideo(url, desc,type) {
    if (isEmpty(url)) {
        layer.msg('暂无数据');
        return;
    }
    var loadingIndex = layer.load(0, { //icon支持传入0-2
        shade: [0.5, 'gray'], //0.5透明度的灰色背景
        content: '下载中...',
        success: function (layero) {
            layero.find('.layui-layer-content').css({
                'padding-top': '39px',
                'padding-left': '12px',
                'width': '60px',
                'color': '#FFFFFF'
            });
        }
    });
    var oReq = new XMLHttpRequest();
    oReq.open("GET", url, true);
    oReq.responseType = "blob";
    oReq.onload = function (oEvent) {
        var content = oReq.response;
        var elink = document.createElement('a');
        elink.download = desc+"."+type;
        elink.style.display = 'none';
        var blob = new Blob([content]);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
        layer.close(loadingIndex);
    };
    oReq.send();
}

$(document).ready(function () {

    //解析视频
    $('.qsy-submit').click(function () {

        $('.qsy-submit').attr('disabled', true);
        var link = rexUrl($('#qsy-url').val());
        if (isEmpty(link)) {
            layer.msg('请输入链接')
            $('.qsy-submit').attr('disabled', false);
            return false;
        }
        var index = layer.load(0);
        $.ajax({
            url: '/parseVideoUrl',
            type: 'POST',
            data: {"link": link},
            success: function (data) {
                $('.qsy-submit').attr('disabled', false);
                try {
                    var code = data.code;
                    if(code != 200){
                        layer.msg("提示："+ data.msg);
                        return;
                    }
                    var rows = data.data;
                    layer.close(index);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 1,
                        shadeClose: true,
                        skin: 'yourclass',
                        content: `<div style="overflow:hidden;height: 580px;width: 350px;">
                                    <div>
                                        <div class="popButton">
                                            <a href="###" rel="noopener nofollow noreferrer" onclick="downloadVideo('${rows.videos[0]['url']}','${rows['title']}','mp4')">
                                                <button class="layui-bg-red layui-btn-sm layui-btn">下载视频</button>
                                            </a>
                                        </div>
                                        <div class="popButton">
                                            <textarea id="videourl" cols="1" rows="1" style="height:0;width:0;position: absolute;">${rows.videos[0]['url']}</textarea>
                                            <button class="layui-btn-sm layui-bg-blue layui-btn" onclick="copy('videourl')">复制链接</button>
                                        </div>
                                        <div class="popButton"><a href="###" rel="noopener nofollow noreferrer" onclick="downloadVideo('${rows.videos[0]['music']}','${rows['title']}','mp3')">
                                            <button class="layui-btn-sm layui-btn">下载音频</button>
                                            </a>
                                        </div>
                                        <video id="video" width="360px" height="550px" src="${rows.videos[0]['url']}" controls = "true" poster="${rows.cover['url']}" preload="auto" webkit-playsinline="true" playsinline="true" x-webkit-airplay="allow" x5-video-player-type="h5" x5-video-player-fullscreen="true" x5-video-orientation="portraint" style="object-fit:fill" autoplay="autoplay">
                                            <source src="${rows.videos[0]['url']}" type="video/mp4"> 
                                        </video>
                                    </div>
                                  </div>`
                    });
                } catch (error) {
                    layer.alert('错误信息:' + error, {
                        title: '异常',
                        skin: 'layui-layer-lan',
                        closeBtn: 0,
                        anim: 4 //动画类型
                    });
                    return false;
                }
            },
            error: function (err) {
                layer.close(index);
                $('.qsy-submit').attr('disabled', false);
            },
            done: function () {
                layer.close(index);
            }
        })
    })

    $('.reset_btn').click(function () {
        $('#qsy-url').val("");
    });
    $('.tool-vip-set').click(function () {
        $('.vip').fadeToggle();
    });
})